package com.xue566.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctc.wstx.util.StringUtil;
import com.xue566.entity.UserEntity;
import com.xue566.enums.StateEnum;
import com.xue566.service.IUserService;
import com.xue566.dao.UserMapper;
import com.xue566.util.ResultBody;
import com.xue566.util.StateException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.management.Query;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    /**
     * @Author: Wuzh
     * @Description: 根据用户参数查询用户记录
     * @Data:
     */
    public ResultBody<UserEntity> selectUser (UserEntity userEntity) {
        if (userEntity == null) {
            throw new StateException(StateEnum.REQUEST_PARAM_EXCE);
        }
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
        if (StringUtils.isNotBlank(userEntity.getUserId()) && null != userEntity.getUserId()) {
            queryWrapper.eq("userId", userEntity.getUserId());
        }
        if (StringUtils.isNotBlank(userEntity.getUserName()) && null != userEntity.getUserName()) {
            queryWrapper.eq("userName", userEntity.getUserName());
        }
        if (StringUtils.isNotBlank(userEntity.getPassWord()) && null != userEntity.getPassWord()) {
            queryWrapper.eq("passWord", userEntity.getPassWord());
        }
        if (StringUtils.isNotBlank(userEntity.getTelPhone()) && null != userEntity.getTelPhone()) {
            queryWrapper.eq("telPhone", userEntity.getTelPhone());
        }
        if (StringUtils.isNotBlank(userEntity.getUserSex()) && null != userEntity.getUserSex()) {
            queryWrapper.eq("userSex", userEntity.getUserSex());
        }
        if (userEntity.getUserAge() > 0 && null != userEntity.getUserSex()) {
            queryWrapper.eq("userAge", userEntity.getUserAge());
        }
        if (StringUtils.isNotBlank(userEntity.getCenterId()) && null != userEntity.getCenterId()) {
            queryWrapper.eq("centerId", userEntity.getCenterId());
        }
        if (StringUtils.isNotBlank(userEntity.getUserIdCard()) && null != userEntity.getUserIdCard()) {
            queryWrapper.eq("userIdCard", userEntity.getUserIdCard());
        }
        if (StringUtils.isNotBlank(userEntity.getUserEntryTime()) && null != userEntity.getUserEntryTime()) { // 入职时间表示一个人,可以相等
            queryWrapper.eq("userEntryTime", userEntity.getUserEntryTime());
        }
        if (StringUtils.isNotBlank(userEntity.getUserEmail()) && null != userEntity.getUserEmail()) {
            queryWrapper.like("userEmail", userEntity.getUserEmail());
        }
        if (StringUtils.isNotBlank(userEntity.getLaborRelations()) && null != userEntity.getLaborRelations()) {
            queryWrapper.eq("laborRelations", userEntity.getLaborRelations());
        }
        if (StringUtils.isNotBlank(userEntity.getBankOfDeposit()) && null != userEntity.getBankOfDeposit()) {
            queryWrapper.like("bankOfDeposit", userEntity.getBankOfDeposit());
        }
        if (StringUtils.isNotBlank(userEntity.getBankCardNumber()) && null != userEntity.getBankCardNumber()) {
            queryWrapper.like("bankCardNumber", userEntity.getBankCardNumber());
        }
        if (StringUtils.isNotBlank(userEntity.getCreateTime()) && null != userEntity.getCreateTime()) {
            queryWrapper.eq("createTime", userEntity.getCreateTime());
        }
        if (StringUtils.isNotBlank(userEntity.getCreateMan()) && null != userEntity.getCreateMan()) {
            queryWrapper.eq("createMan", userEntity.getCreateMan());
        }
        if (StringUtils.isNotBlank(userEntity.getChangeTime()) && null != userEntity.getChangeTime()) {
            queryWrapper.eq("changeTime", userEntity.getChangeTime());
        }
        if (StringUtils.isNotBlank(userEntity.getChangeMan()) && null != userEntity.getChangeMan()) {
            queryWrapper.eq("changeMan", userEntity.getChangeMan());
        }
        if (StringUtils.isNotBlank(userEntity.getUserNation()) && null != userEntity.getUserNation()) {
            queryWrapper.eq("userNation", userEntity.getUserNation());
        }
        if (StringUtils.isNotBlank(userEntity.getIsMarried()) && null != userEntity.getIsMarried()) {
            queryWrapper.eq("isMarried", userEntity.getIsMarried());
        }
        if (StringUtils.isNotBlank(userEntity.getFormalTime()) && null != userEntity.getFormalTime()) {
            queryWrapper.eq("formalTime", userEntity.getFormalTime());
        }
        if (StringUtils.isNotBlank(userEntity.getUserDiploma()) && null != userEntity.getUserDiploma()) {
            queryWrapper.eq("userDiploma", userEntity.getUserDiploma());
        }
        queryWrapper.eq("isUsed", userEntity.getIsUsed());
        userEntity = baseMapper.selectOne(queryWrapper);
        if (userEntity == null) {
            return ResultBody.fail(StateEnum.SELECT_NULL_EXCE.getCode(), StateEnum.SELECT_NULL_EXCE.getMessage());
        }
        return ResultBody.success(userEntity, "user");
    }
}
